<?php

namespace app\admin\controller;

use think\Controller as ThinkController;

/**
 * API控制器基类
 * Class BaseController
 * @package app\store\controller
 */
class Controller extends ThinkController
{
    const JSON_SUCCESS_STATUS = 200;
    const JSON_ERROR_STATUS = 0;

    /**
     * 返回封装后的 API 数据到客户端
     * @param int $status
     * @param string $msg
     * @param array $data
     * @return \think\response\Json
     */
    protected function renderJson($status = self::JSON_SUCCESS_STATUS, $msg = '', $data = [])
    {
        return json(compact('status', 'msg', 'data'));
    }

    /**
     * 返回操作成功json
     * @param string $msg
     * @param array $data
     * @return \think\response\Json
     */
    protected function renderSuccess($data = [], $msg = 'success')
    {
        return $this->renderJson(self::JSON_SUCCESS_STATUS, $msg, $data);
    }

    /**
     * 返回操作失败json
     * @param string $msg
     * @param array $data
     * @return \think\response\Json
     */
    protected function renderError($msg = 'error', $data = [])
    {
        return $this->renderJson(self::JSON_ERROR_STATUS, $msg, $data);
    }

    /**
     * 获取post数据 (数组)
     * @param $key
     * @return mixed
     */
    protected function postData($key)
    {
        return $this->request->post($key . '/a');
    }

    /**
     * APP接口开发中提供的json形式
     * @param string $status
     * @param string $message
     * @param array $date
     * @return false|string
     */
    protected function json($status = '', $message = '', $date = [])
    {
        $result = array(
            'status' => $status,
            'message' => $message,
            'content' => $date
        );
        return json_encode($result, JSON_UNESCAPED_UNICODE);
    }

}
